package cn.demo.service.serviceimpl;

import cn.demo.Util.Page;
import cn.demo.dao.ProviderDao;
import cn.demo.entity.provider;
import cn.demo.service.ProviderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author weizhongli
 * @Date 2020/4/7
 * @Version v1.0
 * @Description 供应商业务接口实现类
 **/
@Service("providerService")
public class ProviderServiceImpl implements ProviderService {

    //依赖注入dao
    @Autowired
    private ProviderDao providerDao;

    /*
    * 添加供应商
    * */
    public int addProvider(provider provider) {
        return providerDao.addProvider(provider);
    }

    /*
     * 查询供应商
     * */
    public Page selectProviderList(int PageIndex, int PageSize, String proName) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("PageIndex", PageIndex * PageSize);
        map.put("PageSize", PageSize);
        map.put("proName", proName);
        Page page = new Page();
        page.setPageIndex(PageIndex);
        page.setPageSize(PageSize);
        int i = providerDao.countProviderList(proName);
        page.setTotalRecords(i);
        int totalPages = page.getTotalRecords() % page.getPageSize() == 0 ? page.getTotalRecords() / page.getPageSize() : page.getTotalRecords() / page.getPageSize() + 1;
        page.setPageTotalpages(totalPages);
        page.setList(providerDao.selectProviderList(map));
        return page;
    }

    public int countProviderList(@Param("proName") String proName) {
        return providerDao.countProviderList(proName);
    }


    /*
     * 修改供应商
     * */
    public int updateProvider(provider provider) {
        return providerDao.updateProvider(provider);
    }

    /*
     * 根据名字查询供应商
     * */
    public provider findByName(String name) {
        return providerDao.findByName(name);
    }

    /*
     * 根据id查询供应商
     * */
    public provider findById(Integer id) {
        return providerDao.findById(id);
    }

    /*
    * 根基id删除供应商
    * */
    public void deleteProvider(Integer id) {
        providerDao.deleteProvider(id);
    }

    public List<provider> selectProList() {
        return providerDao.selectProList();
    }

    public List<Map<String, Object>> selectCountProList() {
        return providerDao.selectCountProList();
    }
}
